package com.xuebank.nfe.nfecar.utils

import android.app.Activity
import android.view.View
import com.afollestad.materialdialogs.MaterialDialog

/**
 * Created by Rona on 2017/4/14.
 */

object DialogUtil {
    fun showAlerDialog(activity: Activity, alertModel: AlertModel, onAlertDialogClickListener: AlertModel.OnAlertDialogClickListener? = null): MaterialDialog {
        val build = MaterialDialog.Builder(activity)

        alertModel.mTitle.let {
            build.title(it)
        }

        alertModel.mMessage.let {
            build.content(it)
        }

        alertModel.mPositiveButtonText.let {
            build.positiveText(alertModel.mPositiveButtonText)
                    .onPositive { dialog, which ->
                        dialog.dismiss()
                        onAlertDialogClickListener?.onPositiveClick()
                    }
        }

        alertModel.mNegativeButtonText.let {
            build.negativeText(alertModel.mNegativeButtonText)
                    .onNegative { dialog, which ->
                        dialog.dismiss()
                        onAlertDialogClickListener?.onNegativeClick()
                    }
        }

        return build.cancelable(false).show()
    }

    fun showCustomDialog(activity: Activity, alertModel: AlertModel, customeView: View, onAlertDialogClickListener: AlertModel.OnAlertDialogClickListener?): MaterialDialog {
        val build = MaterialDialog.Builder(activity)

        alertModel.mTitle.let {
            build.title(it)
        }

        customeView.let {
            build.customView(customeView, false)
        }

        alertModel.mPositiveButtonText.let {
            build.positiveText(alertModel.mPositiveButtonText)
                    .onPositive { dialog, which ->
                        dialog.dismiss()
                        onAlertDialogClickListener?.onPositiveClick()
                    }
        }

        alertModel.mNegativeButtonText.let {
            build.negativeText(alertModel.mNegativeButtonText)
                    .onNegative { dialog, which ->
                        dialog.dismiss()
                        onAlertDialogClickListener?.onNegativeClick()
                    }
        }

        return build.cancelable(false).show()
    }
//    @SuppressLint("ResourceAsColor")
//    fun showUpdateDialog(activity: Activity,
//                         title: String?,
//                         versionName: String?,
//                         updateInfo: String?,
//                         isForceUpdate: Boolean,
//                         actionUpgrade: ((ProgressBar) -> Unit)? = null): MaterialDialog {
//        val commonView = getUpgradeView(activity)
//
//        val dialog: MaterialDialog = DialogUtil.showCustomDialog(activity, AlertModel(null, null, null, null), commonView, null)
//        dialog.window.setBackgroundDrawableResource(android.R.color.transparent)
//        val updateTitle = commonView.findViewById<TextView>(R.id.upgrade_title)
////        val newVersionName = commonView.findViewById<TextView>(R.id.version_name)
//        val updateDesc = commonView.findViewById<TextView>(R.id.upgrade_desc)
//        val upgradeNow = commonView.findViewById<Button>(R.id.bt_upgrade)
//        val cancel = commonView.findViewById<Button>(R.id.btn_cancel)
//        val btnCententView = commonView.findViewById<View>(R.id.btn_centent_view)
//        val btnlayout = commonView.findViewById<LinearLayout>(R.id.btn_layout)
//        val progressBar = commonView.findViewById<SeekBar>(R.id.upgrade_progress)
//        val progressBarDesc = commonView.findViewById<TextView>(R.id.upgrade_progress_desc)
//
//       dialog.setCancelable(false)
//        if (isForceUpdate) {
//            cancel.visibility = View.GONE
//            btnCententView.visibility = View.GONE
//        } else {
//            cancel.visibility = View.VISIBLE
//            btnCententView.visibility = View.VISIBLE
//        }
//
//        if (!title.isNullOrBlank()) {
//            updateTitle.text = title
//        }
//
////        if (!versionName.isNullOrBlank()) {
////            newVersionName.text = versionName
////        }
//
//        if (!updateInfo.isNullOrBlank()) {
//            updateDesc.text = updateInfo
//        }
//
//        progressBar.isEnabled = false
//        progressBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
//            override fun onStartTrackingTouch(seekBar: SeekBar?) {
//            }
//
//            override fun onStopTrackingTouch(seekBar: SeekBar?) {
//            }
//
//            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
//                if (progress >= 100) {
//                    progressBarDesc.text = "新版本安装中"
//
//                    dialog.dismiss()
//                } else {
//                    progressBarDesc.text = "${progress}%"
//                }
//            }
//        })
//
//        RxView.clicks(upgradeNow).throttleFirst(2, TimeUnit.SECONDS).subscribe {
//            actionUpgrade?.invoke(progressBar)
//            updateTitle.text = "努力下载中"
//            upgradeNow.visibility = View.GONE
//            cancel.visibility = View.GONE
//            progressBar.visibility = View.VISIBLE
//            progressBarDesc.visibility = View.VISIBLE
//        }
//
//        RxView.clicks(cancel).throttleFirst(2, TimeUnit.SECONDS).subscribe {
//            dialog.dismiss()
//        }
//
//        return dialog
//    }
    fun showNewDialog(activity: Activity, alertModel: AlertModel, customeView: View, onAlertDialogClickListener: AlertModel.OnAlertDialogClickListener?): MaterialDialog {
        val build = MaterialDialog.Builder(activity)

        alertModel.mTitle.let {
            build.title(it)
        }

        customeView.let {
            build.customView(customeView, false)
        }

        alertModel.mPositiveButtonText.let {
            build.positiveText(alertModel.mPositiveButtonText)
                    .onPositive { dialog, which ->
                        dialog.dismiss()
                        onAlertDialogClickListener?.onPositiveClick()
                    }
        }

        alertModel.mNegativeButtonText.let {
            build.negativeText(alertModel.mNegativeButtonText)
                    .onNegative { dialog, which ->
                        dialog.dismiss()
                        onAlertDialogClickListener?.onNegativeClick()
                    }
        }

        return build.cancelable(false).show()
    }
}
